<?php defined('_JEXEC') or die('Restricted access');
/*	   echo '<pre>';
			print_r($this->regions);
		echo '</pre>';   //*/
 
?>

<table>
        <tr>
            <td width="5">
				<label>Страна</label>
                <!--<input type="text" value="" id="country" name="country" size="70">-->
                <select onChange="location.href='index.php?option=com_qcalc&view=city&country_id='+this.value;">
                   <? foreach ($this->countrys AS $c) { ?>
                      <option value="<?=$c->country_id;?>" <?=( ($c->country_id == $this->country_id) ? "selected='selected'" : "" );?>><?=$c->name;?></option>
                   <? } ?>
                </select>
            </td>
			<td width="5">
				<label>Регион</label>
                <!--<input type="text" value="" id="region" name="region" size="70">-->
                <select onChange="location.href='index.php?option=com_qcalc&view=city&country_id=<?=$this->country_id;?>&region_id='+this.value;">
                   <option value="0">- Все -</option>
                   <? foreach ($this->regions AS $r) { ?>
                      <option value="<?=$r->region_id;?>" <?=( ($r->region_id == $this->region_id) ? "selected='selected'" : "" );?>><?=$r->name;?></option>
                   <? } ?>
                </select>
            </td>
			<td width="5">
				<label>Город</label>
				<input type="text" value="" id="city" name="city" size="70">
          </td><td width="2"><br />
            <button onClick="addCity(this,'<?=$this->country_id;?>','<?=$this->region_id;?>');">Добавить</button>
          </td>
          <td valign="bottom"><nobr> &nbsp; &nbsp; &nbsp; &nbsp; Страницы:</nobr></td><td valign="bottom">
             <? for ( $i=1; $i<=$this->pages; $i++) { ?>
                 <? if ($this->page == ($i-1) ) { ?>
                    <b>[<?=$i;?>]</b>
                 <? } else { ?>
                    <a href="index.php?option=com_qcalc&view=city&country_id=<?=$this->country_id;?>&region_id=<?=$this->region_id;?>&page=<?=($i-1);?>">[<?=$i;?>]</a>
                 <? } ?>
             <? } ?>
         </td>
      </tr>
</table>

<form action="index.php?option=com_qcalc&view=city" method="post" name="adminForm">
<div id="editcell">
    <table class="adminlist">
    <thead>
        <tr>
            <th width="5">
                <?php echo JText::_( 'ID' ); ?>
            </th>
            <th width="20">   <!-- checkbox to check all city -->
                <input type="checkbox" name="toggle" value="" onclick="checkAll( <?php echo count($this->city); ?> );" />
            <!--</th>
            <th width="7%" nowrap="nowrap">
				<label>Включен</label>-->
			</th>
			<!--<th>
                <?php echo JText::_( 'Страна' ); ?>
            </th>-->
			<th width="15%">
                <?php echo JText::_( 'Регион' ); ?>
            </th><th>
                <?php echo JText::_( 'Город' ); ?>
            </th><th width="1">
                &nbsp;
            </th>
            
        </tr>       
    </thead>
 
    <?php
    $k = 0;
 
    foreach ( $this->citys AS $i=>$row) {
    
        $checked = JHTML::_('grid.id', $i, $row->city_id );     //get checkbox HTML
        // get link HTML to edit task for this greeting
        $link = JRoute::_( 'index.php?option=com_qcalc&controller=qcalc&task=edit&cid[]='. $row->city_id );
    ?>
        <tr class="<?php echo "row$k"; ?>">
            <td>
                <?php echo $row->city_id; ?>
            </td>
            <td>
                 <?php echo $checked; ?>
            </td>
            <!--<td align="center">
				<a title="Отключить модуль" onclick="return listItemTask('cb0','unpublish')" href="javascript:void(0);">
				<img border="0" alt="Включен" src="images/tick.png"></a>
			</td>-->
			<!--<td>
                <a href="/<?//php echo $link; ?>"><?//="[". $row->country_id .'] '. $row->c_name;?></a>
            </td>-->
			<td>
                <!--<?="[". $row->region_id ."]";?>--> <?=$row->r_name;?>
            </td><td>
                <a href="/<?php echo $link; ?>"><?php echo $row->name; ?></a>
            </td><td>
                <a href="#" onClick="deleteCity(this,<?=$row->city_id;?>); return false;" style="color:red;" title="Удалить город"> X </a>
            </td>
        </tr>
    <?php
        $k = 1 - $k;    //switch row class
    }
    ?>
    </table>
</div>
<input type="hidden" name="option" value="com_qcalc" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="qcalc" />
</form>


<script>

jQuery(function() {

   jQuery( "#city" ).attr('value','');
   
   jQuery( "#city" ).autocomplete({
            delay:10,
            autoFill:true,
			   minLength: 1,
            selectFirst:true,
            source: "/administrator/components/com_qcalc/assets/javascript/city.js.php?country_id=<?=$this->country_id;?>&region_id=<?=$this->region_id;?>",
            maxItemsToShow:10,
            matchSubset:1,
            cacheLength:10,
            select: function( event, ui ) {
                jQuery(this).attr("city_id",ui.item.id);
			}
		});

});

function addCity(obj,country_id,region_id) {
   var city = jQuery('#city').attr('value');
   if ( region_id <= 0 ) {
      alert('Не выбран регион!'); return false; }
   if ( city.length <= 1 ) {
      alert('Введите название города!'); return false; }

   var data = "name="+city+"&country_id="+country_id+"&region_id="+region_id;
   jQuery.post("index.php?option=com_qcalc&view=city&task=add_city&no_html=1", data, function() {  }, "script");
}

function deleteCity(obj,id) {
   var data = "id="+id;
   jQuery.post("index.php?option=com_qcalc&view=city&task=delete_city&no_html=1", data, function() { jQuery(obj).parent().parent().hide(); }, "script");
}

</script>

<style>

.ui-autocomplete {
   border: solid 1px #999;
}

</style>

